<?php
/*
 * -----------------------------
 * | Rev |    Date    | Author |
 * |  1  | 02-05-2011 | Martin |
 * -----------------------------
 * 
 * DB class is build to represent a DB object.
 * It wil contain all the database functions for al other objects and actions.
 * It will be a static class to gain information from anywhere.
 */
class DB {
    private static $instance	= NULL;    //Instance of class database
    private static $conn	= NULL;    //Connection with the database

    //Create the instance
    private function __construct() {
    	    try {
            self::$conn = new PDO("mysql:host=localhost;dbname=ddpdev", "", ""); // TODO Username & Password!!!
            return $conn;
        } catch (PDOException $e) {
            print("Error connection to database: ".$e->getMessage());
        }
    }

    //Calls the instance
    public static function getInstance() {
        if(self::$instance == NULL) {
            self::$instance = new DB;
        }
    }
    
/* User */
	/*
	 * Get a user by id
	 * @param		$userId		The id of the user
	 * @return		User		The selected user
	 */
	public static function getUser($userId) {
		$prepare	=	self::$conn->prepare("SELECT id, name, passwd FROM user WHERE id = ? LIMIT 1");
		$prepare->bindParam(1, $userId, PDO::PARAM_INT);
		$prepare->execute();
		$row = $prepare->fetch();
		if($row['id'] != NULL) {
			return new User($row['id'], $row['name'], $row['passwd']);
		} 
	}

	/*
	 * Retrieve user by name
	 * @param		$username	The username of an user
	 * @return		User		The user selected on the name
	 */
	public static function getUserByName($username) {
		$prepare	=	self::$conn->prepare("SELECT id, name, passwd FROM user WHERE name = ? LIMIT 1");
		$prepare->bindParam(1, $username, PDO::PARAM_STR);
		$prepare->execute();
		$row = $prepare->fetch();
		if($row['id'] != NULL) {
			return new User($row['id'], $row['name'], $row['passwd']);
		}
	}

	/*
	 * Retrieve a list of all the users
	 * @return		Array(User)	An array with all the users
	 */
	public static function getUserList() {
		$prepare	=	self::$conn->prepare("SELECT id, name, passwd FROM user");
		$prepare->execute();
		foreach($prepare->fetchAll() AS $row) {
			$userList[]	=	new User($row['id'], $row['name'], $row['passwd']);
		}
		return $userList;
	}

	/*
	 * Create User
	 * @param		$user		An user object
	 * @return		Integer		The id of the user that has been created
	 */
	public static function createUser($user) {
		$prepare	=	self::$conn->prepare("INSERT INTO user (name, passwd) VALUES(?,?)");
		$prepare->bindParam(1, $user->getName(), PDO::PARAM_STR);
		$prepare->bindParam(2, $user->getPasswd(), PDO::PARAM_STR);
		$prepare->execute();
		return self::$conn->lastInsertId();
	}

	/*
	 * Update User by Id
	 * @param 		$user		An user object of the user that needs to be updated
	 */
	public static function updateUser($user) {
		$prepare	=	self::$conn->prepare("UPDATE user SET name = ?, passwd = ? WHERE id = ?");
		$prepare->bindParam(1, $user->getName(), PDO::PARAM_STR);
		$prepare->bindParam(2, $user->getPasswd(), PDO::PARAM_STR);
		$prepare->bindParam(3, $user->getId(), PDO::PARAM_INT);
		$prepare->execute();
	}
	
	/*
	 * Delete User by Id
	 * @param		$users		An array with the id's of the users that needs to be deleted
	 */
	public static function deleteUser($users) {
		$prepare	=	self::$conn->prepare("DELETE FROM user WHERE id = ?");
		foreach($users as $id) {
			$prepare->bindParam(1, $id, PDO::PARAM_INT);
			$prepare->execute();
		}
	}

	/*
	 * Truncate the user table
	 */
	public static function truncateUser() {
		$prepare	=	self::$conn->prepare("TRUNCATE user");
		$prepare->execute();
	}
	
/* UserProfile */
	/*
	 * Get a userProfile by id
	 * @param		$userId		The id of the userProfile
	 * @return		UserProfile		The selected userProfile
	 */
	public static function getUserProfile($userId) {
		$prepare	=	self::$conn->prepare("SELECT id, age, sex, avatar FROM userProfile WHERE id = ? LIMIT 1");
		$prepare->bindParam(1, $userId, PDO::PARAM_INT);
		$prepare->execute();
		$row = $prepare->fetch();
		if($row['id'] != NULL) {
			return new UserProfile($row['id'], $row['age'], $row['sex'], $row['avatar']);
		} 
	}

	/*
	 * Create UserProfile
	 * @param		$userProfile		An userProfile object
	 * @return		Integer				The id of the userProfile that has been created
	 */
	public static function createUserProfile($userProfile) {
		$prepare	=	self::$conn->prepare("INSERT INTO userProfile (id, age, sex, avatar) VALUES(?,?,?,?)");
		$prepare->bindParam(1, $userProfile->getId(), PDO::PARAM_INT);
		$prepare->bindParam(2, $userProfile->getAge(), PDO::PARAM_STR);
		$prepare->bindParam(3, $userProfile->getSex(), PDO::PARAM_STR);
		$prepare->bindParam(4, $userProfile->getAvatar(), PDO::PARAM_STR);
		$prepare->execute();
		return self::$conn->lastInsertId();
	}

	/*
	 * Update UserProfile by Id
	 * @param 		$userProfile		An userProfile object of the user that needs to be updated
	 */
	public static function updateUserProfile($userProfile) {
		$prepare	=	self::$conn->prepare("UPDATE userProfile SET age = ?, sex = ?, avatar = ? WHERE id = ?");
		$prepare->bindParam(1, $userProfile->getAge(), PDO::PARAM_STR);
		$prepare->bindParam(2, $userProfile->getSex(), PDO::PARAM_STR);
		$prepare->bindParam(3, $userProfile->getAvatar(), PDO::PARAM_STR);
		$prepare->bindParam(4, $userProfile->getId(), PDO::PARAM_INT);
		$prepare->execute();
	}
	
	/*
	 * Delete UserProfile by Id
	 * @param		$userProfile		An array with the id's of the users that needs to be deleted
	 */
	public static function deleteUserProfile($userProfile) {
		$prepare	=	self::$conn->prepare("DELETE FROM userProfile WHERE id = ?");
		$prepare->bindParam(1, $userProfile->getId(), PDO::PARAM_INT);
		$prepare->execute();
	}

	/*
	 * Truncate the userProfile table
	 */
	public static function truncateUserProfile() {
		$prepare	=	self::$conn->prepare("TRUNCATE userProfile");
		$prepare->execute();
	}
}
DB::getInstance();
?>